package ass.web;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import ass.beans.UserBean;
import ass.dao.UserDAO;
import ass.dao.support.UserDAOImpl;

/**
 * Servlet implementation class EditProfile
 */
public class EditProfile extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public EditProfile() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		HttpSession session = request.getSession(true);
		String username = (String) session.getAttribute("username");
		
		String firstName = (String) request.getParameter("fname");
		String lastName = (String) request.getParameter("lname");
		String yearOfBirth = (String) request.getParameter("yselect");
		String email = (String) request.getParameter("email");
		String[] favGenres = request.getParameterValues("favselect");
		String favActor = (String) request.getParameter("favactor");
		
		
		System.out.println(firstName + " " + lastName + " " + yearOfBirth + " " + email + " "+ favActor);
		
		String favGenre = "";
		for (String x : favGenres){
			favGenre =  x + ","+ favGenre;
		}
		favGenre = favGenre.substring(0, favGenre.length() - 1);
		
		UserDAO userDAO = new UserDAOImpl();
		UserBean userBean = new UserBean();
		
		userBean.setFirst_name(firstName);
		userBean.setLast_name(lastName);
		userBean.setYear_of_birth(yearOfBirth);
		userBean.setEmail(email);
		userBean.setFavourite_genre(favGenre);
		System.out.println(favGenre);
		userBean.setFavourite_actors(favActor);
		
		
		userDAO.update(username, userBean);
		
		request.setAttribute("updated", "<font color='green'> Profile updated </font>");
		String nextJSP = "";
		nextJSP = "/userEditProfile.jsp";
		RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(nextJSP);
		dispatcher.forward(request, response);
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}
